package com.qj.yantaishan.controller;

import com.qj.yantaishan.common.core.domain.R;
import com.qj.yantaishan.common.utils.file.FileUploadUtils;
import com.qj.yantaishan.common.utils.file.FileUtils;
import com.qj.yantaishan.common.utils.uuid.UUID;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/yantaishan/file")
public class FileController {
    @Value("${file.local}")
    private String fileBasePath;

    @PostMapping("/upload")
    public R uploadFile(MultipartFile file) {
        if (file == null) {
            log.error("图片异常");
            return R.fail("上传失败");
        }
        //获取原文件名
        String originalFilename = file.getOriginalFilename();
        log.info("原文件名:" + originalFilename);
        //获取后缀
        String fileSuffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        List<String> suffixList = new ArrayList<>();
        suffixList.add(".png");
        suffixList.add(".jpg");
        suffixList.add(".jpeg");
        suffixList.add(".PNG");
        suffixList.add(".JPG");
        suffixList.add(".JPEG");
        suffixList.add(".mp4");
        suffixList.add(".MP4");

        log.info("文件后缀错误", fileSuffix);
        if (suffixList.indexOf(fileSuffix) == -1) {
            log.info("文件格式错误！");
            return R.fail("文件格式错误");
        }

        //给它随机的文件名
        String randomFileName = UUID.randomUUID().toString() + fileSuffix;
        //文件路径
        String filePath = fileBasePath + randomFileName;
        log.info("文件地址" + filePath);
        //生成保存文件
        File file2 = new File(filePath);
        //保存
        try {
            if (!file2.exists()) {
                //不存在就创建
                file2.createNewFile();
            }
            file.transferTo(file2);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //10.1.2.244
        return R.ok("http://10.1.2.244:10111/yantaishan/file/reload/"+randomFileName);
    }
    @GetMapping("reload/{fileName}")
    public byte[] reloadFile2(@PathVariable String fileName) {

        String filePath = fileBasePath + fileName;
        File file = new File(filePath);
        if (file.exists()) {
            try {
                FileInputStream fileInputStream = new FileInputStream(file);
                byte[] fileBytes = new byte[fileInputStream.available()];
                fileInputStream.read(fileBytes);
                return fileBytes;
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return null;
    }
    @GetMapping("reload")
    public byte[] reloadFile(@RequestParam String fileName) {

        String filePath = fileBasePath + fileName;
        File file = new File(filePath);
        if (file.exists()) {
            try {
                FileInputStream fileInputStream = new FileInputStream(file);
                byte[] fileBytes = new byte[fileInputStream.available()];
                fileInputStream.read(fileBytes);
                return fileBytes;
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return null;
    }
}
